<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类别" prop="applyType">
        <el-select v-model="queryParams.applyType" clearable placeholder="请选择">
          <el-option
            v-for="dict in applyTypes"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="处理状态" prop="handleType">
        <el-select v-model="queryParams.handleType"
                   clearable
                   placeholder="请选择">
          <el-option
            v-for="dict in status"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="展示频道" prop="showPlace">
        <el-select v-model="queryParams.showPlace" clearable placeholder="请选择">
          <el-option
            v-for="dict in showTypes"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-select v-model="queryParams.sort" clearable placeholder="请选择">
          <el-option
            v-for="dict in sortType"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['information:contact:remove']"
          >删除
          </el-button>
        </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contactList" @selection-change="handleSelectionChange" height="410" fixed>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="公司名称" align="center" prop="companyName"/>
      <el-table-column label="意向城市" align="center" prop="intentionCity"/>
      <el-table-column label="所需载体类型" align="center" prop="applyType"/>
      <el-table-column label="姓名" align="center" prop="name"/>
      <el-table-column label="手机号" align="center" prop="phone"/>
      <el-table-column label="处理状态" align="center" prop="handleType">
        <template slot-scope="scope">
          <div v-for="item in status" v-if="item.value === scope.row.handleType">
            <span>{{ item.label }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="处理意见" align="center" prop="handleNote"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180"/>
      <el-table-column label="点击时间" align="center" prop="clickTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.clickTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后登录时间" align="center" prop="loginTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.loginTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleLook(scope.row)"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.handleType==0"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['information:contact:edit']"
          >处理
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改一键联系对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal='false' width="500px" append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="处理状态" prop="handleType">
          <el-select v-model="form.handleType"
                     clearable
                     placeholder="请选择">
            <el-option
              v-for="dict in status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理意见" prop="handleNote">
          <el-input style="width: 213px"  type="textarea" v-model="form.handleNote" placeholder="请输入处理意见"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="open2" :close-on-click-modal='false' width="759px" append-to-body center>
      <el-form ref="form" :model="form"  label-width="130px">
        <el-form-item label="公司名称" prop="companyName">
          <el-input disabled class="kind" v-model="form.companyName" placeholder="请输入公司名称"/>
        </el-form-item>
        <el-form-item label="所在城市" prop="intentionCity">
          <el-input disabled class="kind" v-model="form.intentionCity" placeholder="请输入所在城市"/>
        </el-form-item>
        <el-form-item label="意向需求" prop="showPlace">
          <el-input disabled class="kind" v-model="form.showPlace" placeholder="请输入意向需求"/>
        </el-form-item>
        <el-form-item label="需求类型" prop="applyType">
          <el-input disabled class="kind" v-model="form.applyType" placeholder="请输入需求类型"/>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input disabled class="kind" v-model="form.name" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input disabled class="kind" v-model="form.phone" placeholder="请输入手机号"/>
        </el-form-item>
        <el-form-item label="需求说明" prop="note">
          <el-input disabled class="kind" type="textarea" v-model="form.note" placeholder="请输入需求说明"/>
        </el-form-item>
        <el-form-item label="处理意见" prop="handleNote">
          <el-input disabled class="kind" type="textarea" v-model="form.handleNote" placeholder="请输入处理意见"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addContact, delContact, getContact, listContact, updateContact} from "@/api/information/contact";

export default {
  name: "Contact",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 一键联系表格数据
      contactList: [],
      sortType: [
        {
          value: 0,
          label: "按时间倒序"
        },
        {
          value: 1,
          label: "按时间正序"
        },
      ],
      showTypes: [
        {
          value: "出租",
          label: "出租"
        },
        {
          value: "出售",
          label: "出售"
        },
        {
          value: "转让",
          label: "转让"
        }
        ,
        {
          value: "拍卖",
          label: "拍卖"
        },
        {
          value: "求租",
          label: "求租"
        }
        ,
        {
          value: "求购",
          label: "求购"
        }
      ],
      applyTypes: [
        {
          value: "园区",
          label: "园区"
        },
        {
          value: "楼宇办公",
          label: "楼宇办公"
        },
        {
          value: "楼宇商用",
          label: "楼宇商用"
        },
        {
          value: "仓库",
          label: "仓库"
        },
        {
          value: "厂房",
          label: "厂房"
        },
        {
          value: "公寓",
          label: "公寓"
        },
        {
          value: "拍卖",
          label: "拍卖"
        },
        {
          value: "土地",
          label: "土地"
        }
      ],

      status: [
        {
          value: 0,
          label: "待处理"
        },
        {
          value: 1,
          label: "已处理"
        },
      ],
      status2: [
        {
          value: 0,
          label: "否"
        },
        {
          value: 1,
          label: "是"
        },
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        companyName: null,
        companyScale: null,
        intentionCity: null,
        applyType: null,
        name: null,
        phone: null,
        handleType: null,
        handleNote: null,
        note: null,
        sort: null,
        state: null,
        dr: null,
        clickTime: null,
        loginTime: null,
        showPlace: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询一键联系列表 */
    getList() {
      this.loading = true;
      listContact(this.queryParams).then(response => {
        this.contactList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        companyName: null,
        companyScale: null,
        intentionCity: null,
        applyType: null,
        name: null,
        phone: null,
        handleType: null,
        handleNote: null,
        note: null,
        sort: null,
        state: null,
        dr: null,
        createTime: null,
        updateTime: null,
        clickTime: null,
        loginTime: null,
        showPlace: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加一键联系";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getContact(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改一键联系";
      });
    },
    /** 修改按钮操作 */
    handleLook(row) {
      this.reset();
      const id = row.id || this.ids
      getContact(id).then(response => {
        this.form = response.data;
        this.open2 = true;
        this.title = "查看一键联系";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateContact(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContact(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除一键联系编号为"' + ids + '"的数据项？').then(function () {
        return delContact(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('information/contact/export', {
        ...this.queryParams
      }, `contact_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.kind {
  width: 90%;
}
</style>
